package com.yxkj.syh.app.huarong.activities.orders.return_goods;

import android.app.Application;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;
import androidx.lifecycle.MutableLiveData;

import com.syh.app.basic.base.BaseResponse;
import com.syh.app.basic.base.view_model.BaseViewModel;
import com.syh.app.basic.bindingAdp.view_click.ClickEvent;
import com.syh.app.basic.utils.Tooast;
import com.syh.app.basic.utils.net.ApiObserver;
import com.trello.rxlifecycle2.android.ActivityEvent;
import com.yxkj.syh.app.huarong.bean.Goods;
import com.yxkj.syh.app.huarong.bean.OrderDetail;
import com.yxkj.syh.app.huarong.bean.OrderDetailResponse;
import com.yxkj.syh.app.huarong.bean.OrderInfo;
import com.yxkj.syh.app.huarong.data_center.model.OrderModel;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 创建者：SYH
 * 时间：2020/6/8 14:02
 * 编辑者：SYH、
 * 标签：
 * 补充说明：
 */
public class ReturnGoodsVM extends BaseViewModel<ActivityEvent> {
    private OrderInfo orderInfo;
    private long orderId;

    public ReturnGoodsVM(@NonNull Application application) {
        super(application);
    }

    public ObservableField<String> ofRemark = new ObservableField<>();

    public MutableLiveData<Boolean> mldReturnGoods = new MutableLiveData<>();

    public ClickEvent returnGoods = new ClickEvent() {
        @Override
        public void onClick(View view) {

            if (TextUtils.isEmpty(ofRemark.get())) {
                Tooast.normalInfo("请输入退货说明");
            } else {
                if (orderInfo != null) {
                    editGoods(orderInfo.getDetails());
                } else {
                    if (orderId != 0) {
                        Tooast.normalInfo("请稍后再试");
                        getOrderDetail(orderId);
                    }
                }
            }

        }
    };

    private void returnGoods() {
        Map<String, Object> pMap = new HashMap<>();
        pMap.put("id", orderInfo.getId());
        pMap.put("message", ofRemark.get());
        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("type", 0);
        pMap.put("params", paramsMap);
        showLoading("returnGoods");
        OrderModel.getOrderModel().returnGoods(pMap, mLifecycleProvider.bindToLifecycle(), new ApiObserver<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse response) {
                hideLoading("returnGoods");
                mldReturnGoods.setValue(true);
            }

            @Override
            public void onFailed(int code, String msg) {
                hideLoading("returnGoods");
                Tooast.warning(msg);
            }
        });
    }


    /**
     * 编辑货物
     *
     * @param goodsListP
     */
    public void editGoods(List<Goods> goodsListP) {

        Map<String, Object> pMap = new HashMap<>();
        List<Map<String, Object>> goodsList = new ArrayList<>();
        for (Goods goods : goodsListP) {
            Map<String, Object> goodsMap = new HashMap<>();
            goodsMap.put("settleQuantity", BigDecimal.ZERO);
            goodsMap.put("quantity", goods.getQuantity());
            goodsMap.put("packages", goods.getPackages());
            goodsMap.put("deduct", 100);
            goodsMap.put("deductQuantity", goods.getQuantity());
            goodsMap.put("productNo", goods.getProductNo());
            goodsMap.put("productId", goods.getProductId());
            goodsMap.put("name", goods.getName());
            goodsList.add(goodsMap);
        }
        pMap.put("details", goodsList);
        pMap.put("bankId", orderInfo.getBankId());
        pMap.put("id", orderInfo.getId());

        showLoading("editGoodsOrPayment");
        OrderModel.getOrderModel().editGoodsOrPayment(pMap, mLifecycleProvider.bindToLifecycle(), new ApiObserver<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse response) {
                hideLoading("editGoodsOrPayment");
                returnGoods();
            }

            @Override
            public void onFailed(int code, String msg) {
                hideLoading("editGoodsOrPayment");
                Tooast.warning(msg);
            }
        });
    }

    /**
     * 订单详情
     *
     * @param id
     */
    public void getOrderDetail(long id) {
        this.orderId = id;
        OrderModel.getOrderModel().orderDetail(id, mLifecycleProvider.bindToLifecycle(), new ApiObserver<OrderDetailResponse>() {
            @Override
            public void onSuccess(OrderDetailResponse orderDetailResponse) {
                orderInfo = orderDetailResponse.getData().getOrderInfo();
//                mldOrderDetail.setValue(orderDetailResponse.getData());
            }

            @Override
            public void onFailed(int code, String msg) {
                Tooast.warning(msg);
            }
        });
    }

}
